﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NextCMS.Models;

public partial class WebAdmin_AddPage : WebadminBasePage
{
    int Id = 0;
    int PageLanguageId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["Id"]), out Id);
        int.TryParse(Convert.ToString(Request.QueryString["PageLanguageId"]), out PageLanguageId);
        if (!IsPostBack)
        {
            BindMasterPages();
            if (Id > 0)
            {
                PagesHelper cc = new PagesHelper();

                PageModel mp = cc.GetByPageLanguageId(PageLanguageId);
                if (mp != null)
                {
                    txtTitle.Text = mp.Title;
                    txtFileName.Text = mp.FileName;
                    txtMetaKeywords.Text = mp.MetaKeywords;
                    txtMetaDescriptions.Text = mp.MetaDescription;
                    DDLMasterPage.SelectedValue = mp.MasterPageId.ToString();
                    
                }
                else
                {
                    mp= cc.GetByPageId(Id);
                    txtFileName.Text = mp.FileName;
                }
                ltrtitle.Text = "Edit Page";
                if (SiteSettingsHelper.GetSetting("HomePage",SiteId) == Id.ToString())
                    chkMakeHomePage.Checked = true;
            }
            else
            {
                ltrtitle.Text = "Create New Page";
            }
        }
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-1.7.js", ResolveClientUrl("~/Resources/Javascript/jquery-1.7.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-ui-1.8.4.custom.min.js", ResolveClientUrl("~/FileManager/js/jquery-ui-1.8.4.custom.min.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery.ui.message.js", ResolveClientUrl("~/Resources/Javascript/jquery.ui.message.js"));
        AddCssLink("FileManager/css/ui-lightness/jquery-ui-1.8.4.custom.css");

    }
    private void BindMasterPages()
    {
        TemplatesHelper mp = new TemplatesHelper();
        DDLMasterPage.DataSource = mp.GetAllBySiteId(SiteId);
        DDLMasterPage.DataTextField = "Name";
        DDLMasterPage.DataValueField = "Id";
        DDLMasterPage.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        PagesHelper cc = new PagesHelper();
        PageModel cModel = new PageModel();

        if (cc.GetPageByFileName(txtFileName.Text, Id) == null)
        {
            cModel.FileName = txtFileName.Text;
            cModel.Title = txtTitle.Text;
            cModel.MasterPageId = Convert.ToInt32(DDLMasterPage.SelectedValue);
            cModel.MetaKeywords = txtMetaKeywords.Text;
            cModel.MetaDescription = txtMetaDescriptions.Text;
            cModel.Id = Id;
            cModel.PageLanguageId = PageLanguageId;
            cModel.LanguageId = LanguageId;
            cModel.SiteId = SiteId;
            if (Id > 0)
                cc.Edit(cModel);
            else
                Id = cc.Save(cModel);

            if (chkMakeHomePage.Checked)
                SiteSettingsHelper.SetSetting("HomePage", Id.ToString(),SiteId);
            Response.Redirect("ManagePages.aspx");
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "f", @"$('#message1').message({ dismiss: false, type: 'error', message: 'File Name already exists in system.' });", true);
        }
    }
}